package com.ace.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ace.model.Ace;
import com.ace.service.IAceService;

@RequestMapping(value = "/ace")
@Controller
public class AceEditorController extends BaseController{

  	public static final String AJAX_SUCC = "OK";
  	public static final String AJAX_FAIL = "FAIL";
  	
    @Autowired
    IAceService iAceService;
    
    /**
     * 列表页面
     * @param model
     * @param id
     * @return
     * @author Josh
     */
    @RequestMapping(value = "/list")
    public String list(Model model){
    	try {
    		List<Ace> list = iAceService.getList();
    		model.addAttribute("list", list);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return "/ace/list";
    }
  	
    /**
     * 新增/修改页面
     * @param model
     * @param id
     * @return
     * @author Josh
     */
    @RequestMapping(value = "/add")
    public String add(Model model, @RequestParam(value = "id", required = false) Integer id){
    	try {
    		if(id != null && id.intValue() > 0){
	    		Ace ace = iAceService.selectByPrimaryKey(id);
	    		model.addAttribute("ace", ace);
    		}
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return "/ace/add";
    }
    
    /**
     * 保存方法
     * @param model
     * @param id
     * @param content
     * @return
     * @author Josh
     */
    @RequestMapping(value = "/add_save")
    public String addSave(Model model, 
    		@RequestParam(value = "id", required = false) Integer id,
    		@RequestParam(value = "title", required = false) String title,
    		@RequestParam(value = "content", required = false) String content) {
        
        try {
        	if(id != null && id.intValue() > 0 
        			&& StringUtils.isNotBlank(title) 
        			&& StringUtils.isNotBlank(content)){
        		
        		Ace ace = iAceService.selectByPrimaryKey(id);
        		ace.setTitle(title);
        		ace.setContent(content);
	            if (iAceService.update(ace) > 0) {
	            	return "redirect:list.do";
	            }
        	}else if((id == null || id.intValue() <= 0) 
        			&& StringUtils.isNotBlank(title) 
        			&& StringUtils.isNotBlank(content)){
        		
        		Ace ace = new Ace();
        		ace.setTitle(title);
        		ace.setContent(content);
        		if(iAceService.save(ace) > 0){
        			return "redirect:list.do";
        		}
        	}
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:list.do";
    }
    
    /**
     * 查看页面
     * @param model
     * @param id
     * @return
     * @author Josh
     */
    @RequestMapping(value = "/view")
    public String view(Model model, @RequestParam(value = "id", required = false) Integer id){
    	try {
    		if(id != null && id.intValue() > 0){
	    		Ace ace = iAceService.selectByPrimaryKey(id);
	    		model.addAttribute("ace", ace);
    		}
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return "/ace/view";
    }
    
    /**
     * 删除方法
     * @param model
     * @param id
     * @return
     * @author Josh
     */
    @RequestMapping(value = "/del")
    public String del(Model model, @RequestParam(value = "id", required = false) Integer id){
    	try {
    		if(id != null && id.intValue() > 0){
	    		iAceService.delete(id);
    		}
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return "redirect:list.do";
    }
}